<?php

$imgArr = $_POST['imgArr'];

define('DS', DIRECTORY_SEPARATOR);
$root = $_SERVER['DOCUMENT_ROOT'];
$root = str_replace('\\', DS, $root);
$root = str_replace('/', DS, $root);
define('ROOT', $root);
unset($root);
define('DATA_DIR', ROOT.DS.'data'.DS);

$name = array();
foreach($imgArr as $img) {
  array_push($name, editorImg($img));
}
print_r(implode(',', $name));

function editorImg($url) {
  $path = DATA_DIR.'upload'.DS;
  if(is_dir($path)) {
    $path = $path.'imageOut'.DS;
    if(!is_dir($path)) mkdir($path);
    $path = $path.date('Ymd').DS;
    if(!is_dir($path)) mkdir($path);
    
    $nowNum = time();
    for($i = 0; $i < 10; $i++) {
      if(!is_file($path.$nowNum.'.png')) break;
      $nowNum++;
    }
    $name = $nowNum.'.png';
    downloadImg($url, $path, $name);
    $file = str_replace(ROOT, '', $path).$name;
    return $file;
  }
}

function downloadImg($url, $path, $name) {
  if(!$url) exit('The image is not Exists!!!');
  if(!$path) exit('The path is not Exists!!!');

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  $file = curl_exec($ch);
  curl_close($ch);
  $resource = fopen($path.$name, 'a');
  fwrite($resource, $file);
  fclose($resource);
}